<template>
  <div class="login-container">
    <!-- 装饰元素 -->
    <div class="decoration-circle circle-1"></div>
    <div class="decoration-circle circle-2"></div>
    <div class="decoration-circle circle-3"></div>
    
    <div class="login-box">
      <div class="login-header">
        <div class="logo-icon">✨ CMDB ✨</div>
        <h1>资产管理系统</h1>
        <p>🌸 欢迎回来 ~ 🌸</p>
      </div>
      
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label>用户名</label>
          <input 
            v-model="formData.username" 
            type="text" 
            placeholder="请输入用户名"
            required 
          />
        </div>
        
        <div class="form-group">
          <label>密码</label>
          <input 
            v-model="formData.password" 
            type="password" 
            placeholder="请输入密码"
            required 
          />
        </div>
        
        <div v-if="error" class="error-message">
          {{ error }}
        </div>
        
        <button type="submit" class="btn-login" :disabled="loading">
          <span v-if="loading" class="loading-spinner"></span>
          {{ loading ? '登录中...' : '🚀 立即登录' }}
        </button>
        
        <div class="login-footer">
          <span>还没有账号？</span>
          <router-link to="/register" class="link">💖 立即注册</router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import api from '../services/api'

const router = useRouter()

const formData = ref({
  username: '',
  password: ''
})

const loading = ref(false)
const error = ref('')

const handleLogin = async () => {
  loading.value = true
  error.value = ''
  
  try {
    const response = await api.login(formData.value)
    
    // 保存token和用户信息
    localStorage.setItem('token', response.token)
    localStorage.setItem('user', JSON.stringify(response.user))
    
    // 跳转到首页
    router.push('/dashboard')
  } catch (err) {
    error.value = err.response?.data?.error || err.message || '登录失败，请重试'
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #fef5f8 50%, #e8f5ff 100%);
  position: relative;
  overflow: hidden;
}

.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 20% 30%, rgba(255, 107, 157, 0.15) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(196, 113, 237, 0.15) 0%, transparent 50%),
    radial-gradient(circle at 50% 50%, rgba(18, 202, 214, 0.1) 0%, transparent 50%);
  pointer-events: none;
}

/* 装饰圆圈 */
.decoration-circle {
  position: absolute;
  border-radius: 50%;
  opacity: 0.3;
  animation: float 8s ease-in-out infinite;
}

.circle-1 {
  width: 300px;
  height: 300px;
  background: linear-gradient(135deg, rgba(255, 107, 157, 0.2), rgba(196, 113, 237, 0.2));
  top: -100px;
  left: -100px;
  animation-delay: 0s;
}

.circle-2 {
  width: 200px;
  height: 200px;
  background: linear-gradient(135deg, rgba(18, 202, 214, 0.2), rgba(77, 171, 247, 0.2));
  bottom: -80px;
  right: -80px;
  animation-delay: 2s;
}

.circle-3 {
  width: 150px;
  height: 150px;
  background: linear-gradient(135deg, rgba(255, 169, 77, 0.2), rgba(255, 193, 7, 0.2));
  top: 50%;
  left: 10%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-30px) rotate(180deg);
  }
}

.login-box {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 450px;
  padding: 50px 40px;
  background: rgba(255, 255, 255, 0.95);
  border: none;
  border-radius: 30px;
  box-shadow: 
    0 20px 60px rgba(196, 113, 237, 0.2),
    0 8px 30px rgba(255, 107, 157, 0.15),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(20px);
  animation: slideUp 0.6s ease-out;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #ff6b9d, #c471ed, #12cad6, #ff6b9d);
  border-radius: 30px 30px 0 0;
  background-size: 200% 100%;
  animation: gradientShift 3s ease infinite;
}

@keyframes gradientShift {
  0%, 100% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.logo-icon {
  font-size: 32px;
  font-weight: 900;
  background: linear-gradient(135deg, #ff6b9d, #c471ed);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 10px;
  letter-spacing: 3px;
  animation: sparkle 2s ease-in-out infinite;
}

@keyframes sparkle {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.8; transform: scale(1.05); }
}

.login-header h1 {
  font-size: 24px;
  color: #2e3440;
  margin-bottom: 8px;
  font-weight: 700;
}

.login-header p {
  color: #6c7589;
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  color: #ff6b9d;
  font-size: 14px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.form-group label::before {
  content: '🌸';
  font-size: 14px;
}

.form-group input {
  padding: 14px 18px;
  background: #ffffff;
  border: 2px solid #e9ecef;
  border-radius: 15px;
  color: #2e3440;
  font-size: 14px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.form-group input:focus {
  outline: none;
  border-color: #ff6b9d;
  box-shadow: 0 0 0 4px rgba(255, 107, 157, 0.1), 0 4px 12px rgba(196, 113, 237, 0.15);
  background: #fef5f8;
  transform: translateY(-2px);
}

.form-group input::placeholder {
  color: #adb5bd;
}

.error-message {
  padding: 14px;
  background: linear-gradient(135deg, rgba(255, 107, 107, 0.1), rgba(255, 135, 135, 0.05));
  border: 2px solid #ff6b6b;
  border-radius: 15px;
  color: #ff6b6b;
  font-size: 14px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  animation: shake 0.5s ease;
}

.error-message::before {
  content: '⚠️';
  font-size: 18px;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-10px); }
  75% { transform: translateX(10px); }
}

.btn-login {
  padding: 16px;
  background: linear-gradient(135deg, #ff6b9d, #c471ed);
  border: none;
  border-radius: 25px;
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
  box-shadow: 0 4px 20px rgba(255, 107, 157, 0.3);
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-login::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s ease;
}

.btn-login:hover:not(:disabled)::before {
  left: 100%;
}

.btn-login:hover:not(:disabled) {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 8px 30px rgba(196, 113, 237, 0.4);
  background: linear-gradient(135deg, #c471ed, #ff6b9d);
}

.btn-login:active:not(:disabled) {
  transform: translateY(-1px) scale(0.98);
}

.btn-login:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background: linear-gradient(135deg, #adb5bd, #868e96);
}

.loading-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.login-footer {
  text-align: center;
  color: #6c7589;
  font-size: 14px;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 2px dashed #e9ecef;
}

.link {
  color: #ff6b9d;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 600;
  transition: all 0.3s ease;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.link:hover {
  color: #c471ed;
  transform: translateX(3px);
}

@media (max-width: 480px) {
  .login-box {
    margin: 20px;
    padding: 40px 25px;
  }
  
  .logo-icon {
    font-size: 28px;
  }
  
  .login-header h1 {
    font-size: 20px;
  }
  
  .decoration-circle {
    opacity: 0.2;
  }
}
</style>
